import * as lib from '../../../components/aotoo/lib/index'
const dateInfoHooks = lib.hooks('date-info', true)
import {diffCycleDays} from './util'
export default function({tapDay, today, funButtons, record, caseKey}){
  const $$ = wx.calendarInst.getDateHandle
  const that = this
  const _ = this._
  const key = caseKey.split('_')[0]
  record = this._getItem(key);

  function resetMenstruation(content){
    const pitsList = that.getElementsById('pits-list')
    const oldDate = pitsList.find({caseKey: key})
    if (oldDate && (oldDate.length||oldDate.data.length)) {
      const prevData = oldDate.data[0].data
      const prevDate = prevData.date
      const interval = prevDate.interval || '5'
      if (prevDate) {
        const tempDate = _.getYmd(prevDate)
        const rangeSelectedDate = [tempDate]
        for (let ii=0; ii<parseInt(interval); ii++) {
          ii>0 && rangeSelectedDate.push(tempDate.offset(ii))
        }
        rangeSelectedDate.forEach( item => {
          if (item.timestr) {
            const inst = $$(item.timestr)
            if (inst) {
              inst.removeClass('active', function(){
                inst.removeClass('menstruation')
              }) 
            }
          }
        })
      }
    }
    pitsList.delete({caseKey: 'menstruation'})

    setTimeout(() => {
      const interval = content.interval || '5'
      const timestr = tapDay.timestr
      const tempDate = _.getYmd(timestr)
      const rangeSelectedDate = [tempDate]
      for (let ii=0; ii<parseInt(interval); ii++) {
        ii > 0 && rangeSelectedDate.push(tempDate.offset(ii))
      }
      rangeSelectedDate.forEach( item => {
        if (item.timestr) {
          const inst = $$(item.timestr)
          if (inst) {
            inst.addClass('menstruation active')
          }
        }
      })
    }, 300);
  }

  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key
    }
    return item
  })

  const {diffDays, nextDate} = diffCycleDays(tapDay, today)
  tapDay.diffDays = diffDays

  const title = `每月${tapDay.day}日`
  const content = {
    body: [`${diffDays}`, title, '大姨妈'], 
    caseKey: 'menstruation', 
    date: tapDay.timestr, 
    itemClass: 'menstruation', 
    targetDay: tapDay, 
    interval: '5',   // 经期周期是多少天
    ...funButtons
  }

  function beforeCallback({createCaseCard}){
    if (record) {
      const date = record.date
      const dateObj = _.getYmd(date)
      let   title = []
      let diffDay = dateObj.day - today.day
      if (diffDay > 0) {
        const lDate = _.lunar.solar2lunar(today.year, today.month, dateObj.day)
        title = [
          '下一周期还有', 
          {title: `${diffDay}`, itemClass: 'inner-content-case-title-day'}, 
          `那天是${lDate.ncWeek}`
        ]
      } else if(diffDay === 0) {
        title = ['亲戚访问中', '😎']
      } else {
        diffDay = dateObj.day + (dateObj.days - dateObj.day + diffDay)
        const nextDate = today.offset(diffDay)
        const lDate = _.lunar.solar2lunar(nextDate.year, nextDate.month, nextDate.day)
        title = [
          '下一周期还有', 
          {title: `${diffDay}`, itemClass: 'inner-content-case-title-day'}, 
          `那天是${lDate.ncWeek}`
        ]
      }
      wx.modalx.showModal({
        content: {
          title, 
          body: [
            `已设置经期起始日为每月 ${dateObj.day} 号`,
            {
              title: `周期：`,
              "@list": {
                data: [
                  {title: '5天'}, 
                  {title: '6天'}, 
                  {title: '7天'}, 
                ], 
                listClass: 'menstruation-checkList', 
                itemClass: 'menstruation-checkItem',
                itemMethod: {
                  aim(e, param, inst){
                    const index = inst.index
                    inst.siblings().removeClass('active', function(){
                      inst.addClass('active');
                    })
                    content.interval = (['5天', '6天', '7天'][index] || '5天').replace('天', '')
                  }
                },
                ready(){
                  const checkedIt = this.find({title: record.interval+'天'})
                  checkedIt && checkedIt.addClass('active')
                }
              },
              itemStyle: 'display: flex; align-items: center; height: 2.7em;',
            },
            {
              title: `点击<<重设>>切换经期日每月 ${tapDay.day} 号，当前选定日期 ${tapDay.timestr}`,
              itemStyle: 'font-size: 12px;'
            }, 
          ],
          bodyStyle: 'height: 120px; display: flex; flex-direction: column;',
          itemClass: 'inner-content',
        },
        confirmText: '重设',
        cancelText: '取消', 
        height: 50,
        width: 70,
        itemClass: 'popup menstruation', 
        mask: true,
        success(res){
          if (res.confirm) {
            resetMenstruation(content)
            createCaseCard(content)
            that.restoreCaseButton()
          }
        }
      })
    } else {
      wx.modalx.showModal({
        content: {
          title: [' ', '🙅🏻‍♀️'], 
          body: [
            {
              title: '设置周期: ', 
              // "@switch": { checked: false, itemStyle: 'margin-left: 1em', bindchange: 'onIsHideMemo'}, 
              "@list": {
                data: [{title: '5天', itemClass: 'active'}, '6天', '7天'], 
                listClass: 'menstruation-checkList', 
                itemClass: 'menstruation-checkItem',
                itemMethod: {
                  aim(e, param, inst){
                    const index = inst.index
                    inst.siblings().removeClass('active', function(){
                      inst.addClass('active');
                    })
                    content.interval = (['5天', '6天', '7天'][index] || '5天').replace('天', '')
                  }
                }
              },
              itemStyle: 'display: flex; align-items: center; height: 2.7em;',
            },
            `点击<<设置>>将薪水日指定为每月 ${tapDay.day} 号，当前选定日期 ${tapDay.timestr}`, 
          ],
          itemClass: 'inner-content',
        },
        confirmText: '设置',
        cancelText: '取消', 
        height: 40,
        width: 70,
        itemClass: 'popup menstruation', 
        mask: true,
        success(res){
          if (res.confirm) {
            resetMenstruation(content)
            createCaseCard(content)
          }
          that.restoreCaseButton()
        }
      })
    }
  }
  
  return {key, content, beforeCallback}
}